﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class ChiTietPhieuDAO
    {
        public DataTable LayTable()
        {
            String sql = "SELECT * from ChiTietPhieu ";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTheoMaPhieu(PhieuNhapDTO pn)
        {
            String sql = "SELECT ct.MaChitiet,pn.MaPhieuNhap,s.TenSach,SoLuongNhap,NgayNhap,s.SoLuong,s.MaSach from ChiTietPhieu ct,PhieuNhap pn ,Sach s where pn.MaPhieuNhap=ct.MaPhieuNhap and ct.MaSach = s.MaSach and ct.MaPhieuNhap =" + pn.MaPN;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void them(ChiTietPhieuNhapDTO pn)
        {
            String sql = "Insert into ChiTietPhieu(MaPhieuNhap,MaSach,SoLuongNhap) values(" + pn.MaPN + "," +pn.MaSach + "," + pn.SoLuongNhap + ")";
            SqlConnection con = DataProvider.ConnectDB();
            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }

        public void sua(ChiTietPhieuNhapDTO pn)
        {
            String sql = "UPDATE ChiTietPhieu SET MaPhieuNhap=" + pn.MaPN + ",MaSach=" + pn.MaSach + ",SoLuongNhap=" + pn.SoLuongNhap + "where MaChitiet =" + pn.MaChiTiet;
            SqlConnection con = DataProvider.ConnectDB();
            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public void xoa(ChiTietPhieuNhapDTO pn)
        {
            String sql = "DELETE from  ChiTietPhieu where MaChitiet =" + pn.MaChiTiet;
            SqlConnection con = DataProvider.ConnectDB();
            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
    }
}
